# -*- coding: utf-8 -*-
from django import forms
from django.forms import ModelForm
from django.contrib.auth.models import User
from b52.home.models import UserProfile

class RegistrationForm(ModelForm):
    username  = forms.CharField(max_length=30)
    #password2   = forms.CharField(label=(u'Имя польssзователя'), max_length=30)
    email     = forms.EmailField(label=(u'E-mail'))
    password = forms.CharField(widget=forms.PasswordInput(render_value=False), max_length=30)
    password1 = forms.CharField(widget=forms.PasswordInput(render_value=False), max_length=30)
    
    class Meta:
        model   = UserProfile
        exclude = ('user')
        
    def clean_username(self):
        username = self.cleaned_data['username']
        try:
            User.objects.get(username=username)
        except User.DoesNotExist:
            return username
        raise forms.ValidationError('Такой пользователь существует')
        
    #TODO some trouble with it, check in vid 7
      
    def clean(self):
        if 'password' in self.cleaned_data and 'password1' in self.cleaned_data:
            if self.cleaned_data['password'] != self.cleaned_data['password1']:
                raise forms.ValidationError("You must type the same password each time")
        return self.cleaned_data
  
        
    #def clean_password2(self):
    #    #password1 = self.cleaned_data['password1']
    #    password2 = self.cleaned_data['password2']
    #    
    #    #if password1 != password2:
    #    #    raise forms.ValidationError('Пароли не совпадают')
    #        
    #    return password2
    
    #def save(self):
    #    User.objects.create_user
    #    new_user = User.objects.cteate_user(username=self.changed_data['username'],
    #                                        email=self.cleaned_data['email'],
    #                                        password=self.cleaned_data['password1'])
    #    return new_user
        
        
        
        
        